
package com.jf.cloud.multishop.controller.multishop;

import com.jf.cloud.api.auth.feign.AccountFeignClient;
import com.jf.cloud.api.auth.vo.AuthAccountVO;
import com.jf.cloud.api.multishop.constant.ShopStatus;
import com.jf.cloud.api.multishop.vo.ShopDetailVO;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.multishop.constant.AuditStatus;
import com.jf.cloud.multishop.service.ShopAuditingService;
import com.jf.cloud.multishop.service.ShopDetailService;
import com.jf.cloud.multishop.vo.ShopAuditingVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author zz
 */
@RestController("multishopShopApplyController")
@RequestMapping("/m/apply_shop/shop_apply")
@Tag(name = "multishop-店铺申请")
public class ShopApplyController {
    private final Logger LOGGER = LoggerFactory.getLogger(ShopApplyController.class);
    @Autowired
    private ShopDetailService shopDetailService;

    @Autowired
    private ShopAuditingService shopAuditingService;

    @Autowired
    private AccountFeignClient accountFeignClient;

    @GetMapping("/get_auditing_info")
    @Operation(summary = "获取店铺审核状态信息" , description = "获取店铺审核状态信息,返回值为空代表该店铺没有提交过审核信息")
    public ServerResponseEntity<ShopAuditingVO> getAuditingInfo() {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        if (Objects.equals(shopId, Constant.DEFAULT_SHOP_ID)) {
            LOGGER.info("获取店铺审核状态信息,该店铺没有提交过审核信息");
            return ServerResponseEntity.success(null);
        }
        ShopAuditingVO shopAuditingVO = shopAuditingService.getByshopId(shopId);
        if (Objects.isNull(shopAuditingVO)) {
            return ServerResponseEntity.success(null);
        }
        ServerResponseEntity<AuthAccountVO> responseEntity = accountFeignClient.getAccountInfoByTenantId(shopId, sysType);
        ShopDetailVO shopDetailVO = shopDetailService.getByShopId(shopId);
        if (Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.OFFLINE.value())) {
            shopAuditingVO.setStatus(AuditStatus.OFFLINE.value());
        } else if (Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.OFFLINE_AWAIT_AUDIT.value()) || Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.OPEN_AWAIT_AUDIT.value())) {
            shopAuditingVO.setStatus(AuditStatus.WAITAUDIT.value());
        }
        if (responseEntity.isSuccess() && Objects.nonNull(responseEntity.getData())) {
            AuthAccountVO authAccountVO = responseEntity.getData();
            shopDetailVO.setMerchantAccount(authAccountVO.getPhone());
            shopDetailVO.setAccountStatus(authAccountVO.getStatus());
        }
        shopAuditingVO.setShopDetail(shopDetailVO);
        shopAuditingVO.setUserId(null);
        shopAuditingVO.setShopId(null);
        shopAuditingVO.setAuditorId(null);
        return ServerResponseEntity.success(shopAuditingVO);
    }

}
